﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using System.Drawing.Design;
using System.Web.UI.HtmlControls;
using System.Data;

namespace jQuery.WebControls.Charts
{
    /// <summary>
    /// 
    /// </summary>
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:jQueryChart runat=server></{0}:jQueryChart>")]    
    public class jQueryChart : WebControl
    {        
        private DataTable dataSource;
        private string chartTitle;
        private string xAxeTitle, yAxeTitle;
        private string xDataMember, yDataMember, dataSeries;
        private string[] dataX, dataY;

        #region PROPERTIES
        public bool AllowExportChart
        {
            get
            {
                if (ViewState["exp"] != null)
                {
                    return bool.Parse(ViewState["exp"].ToString());
                }
                else
                    return false;
            }
            set
            {
                ViewState["exp"] = value;
            }
        }

        public ChartTypes ChartType
        {
            get
            {
                if (ViewState["cType"] == null)
                {
                    ViewState.Add("cType", ChartTypes.Pie);
                }               
                return (ChartTypes)ViewState["cType"];
            }
            set
            {
                ViewState["cType"] = value;
            }
        }
        
        public string ChartTitle
        {
            get 
            { 
                if(String.IsNullOrEmpty(chartTitle))
                {
                    if(ViewState["ttlch"] != null)
                        chartTitle= ViewState["ttlch"].ToString();
                }
                return chartTitle;
            }
            set 
            { 
                ViewState["ttlch"] = value; 
                chartTitle = value;
            }
        }

        public string X_AxeTitle
        {
            get 
            { 
                if(String.IsNullOrEmpty(xAxeTitle))
                {
                    if(ViewState["xtlch"] != null)
                        xAxeTitle = ViewState["xtlch"].ToString();
                }
                return xAxeTitle;
            }
            set 
            { 
                ViewState["xtlch"] = value; 
                xAxeTitle = value;
            }
        }

        public string Y_AxeTitle
        {
            get 
            { 
                if(String.IsNullOrEmpty(yAxeTitle))
                {
                    if(ViewState["ytlch"] != null)
                        yAxeTitle= ViewState["ytlch"].ToString();
                }
                return yAxeTitle;
            }
            set 
            { 
                ViewState["ytlch"] = value; 
                yAxeTitle = value;
            }
        }        
        #endregion

        #region DATASOURCE PROPERTIES
        public DataTable DataSource
        {
            get
            {
                if (dataSource == null)
                {
                    if (ViewState["ds"] != null)
                        return ViewState["ds"] as DataTable;
                }
                return dataSource;
            }
            set
            {
                ViewState["ds"] = value;
                dataSource = value;
            }
        }

        public string DataFieldMemberX_Axe
        {
            get
            {
                if (String.IsNullOrEmpty(xDataMember))
                {
                    if (ViewState["xDFM"] != null)
                        xDataMember = ViewState["xDFM"].ToString();
                }
                return xDataMember;
            }
            set
            {
                ViewState["xDFM"] = value;
                xDataMember = value;
            }
        }

        public string DataFieldMemberY_Axe
        {
            get
            {
                if (String.IsNullOrEmpty(yDataMember))
                {
                    if (ViewState["yDFM"] != null)
                        xDataMember = ViewState["yDFM"].ToString();
                }
                return yDataMember;
            }
            set
            {
                ViewState["yDFM"] = value;
                yDataMember = value;
            }
        }

        public string DataFieldMember_Series
        {
            get
            {
                if (String.IsNullOrEmpty(dataSeries))
                {
                    if (ViewState["dFM"] != null)
                        dataSeries = ViewState["dFM"].ToString();
                }
                return dataSeries;
            }
            set
            {
                ViewState["dFM"] = value;
                dataSeries = value;
            }
        }
        #endregion

        public void DataBinding()
        {
            
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string script = "var chart;";
            script += "$(document).ready(function(){";
            script += "chart = new Highcharts.Chart({";
            script += "chart: {";
            script += "renderTo: 'dv" + ClientID + "',";
            script += "defaultSeriesType: 'line',";
            script += "marginRight: 130,";
            script += "marginBottom: 25";
            script += "},";
            script += "title: {";
            script += "text: 'Monthly Average Temperature',";
            script += "x: -20";
            script += "},";
            script += "subtitle: {";
            script += "text: 'Source: WorldClimate.com',";
            script += "x: -20";
            script += "},";
            script += "xAxis: {";
            script += "categories: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']";
            script += "},";
            script += "yAxis: {";
            script += "title: {";
            script += "text: 'Temperature (Â°C)'";
            script += "},";
            script += "plotLines: [{";
            script += "value: 0,";
            script += "width: 1,";
            script += "color: '#808080'";
            script += "}]";
            script += "},";
            script += "tooltip: {";
            script += "formatter: function() {";
            script += "return '<b>'+ this.series.name +'</b><br/>'+";
            script += "this.x +': '+ this.y +'Â°C';";
            script += "}";
            script += "},";
            script += "legend: {";
            script += "layout: 'vertical',";
            script += "align: 'right',";
            script += "verticalAlign: 'top',";
            script += "x: -10,";
            script += "y: 100,";
            script += "borderWidth: 0";
            script += "},";
            script += "series: [{";
            script += "name: 'Tokyo',";
            script += "data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]";
            script += "}, {";
            script += "name: 'New York',";
            script += "data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]";
            script += "}, {";
            script += "name: 'Berlin',";
            script += "data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]";
            script += "}, {";
            script += "name: 'London',";
            script += "data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]";
            script += "}]";
            script += "});";
            script += "});";

            ClientScriptProxy.Current.RegisterClientScriptBlock(this, this.GetType(), 
                String.Format("chart_{0}", ID), script, true);
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (Visible)
            {
                writer.Write(String.Format("<div id='dv{0}'></div>", ClientID));
            }
        }
        
        /// <summary>
        /// Check if there is a jQueryScriptManager control in the page
        /// </summary>
        /// <param name="e">Event argument</param>        
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!Page.IsPostBack)
            {
                if (!DesignMode)
                    CheckQueryManager();
            }
        }

        private void CheckQueryManager()
        {
            Common.Instance.CheckJQueryScriptManager(Page);
        } 
    }
}